package com.xdap.common.utils;

import com.xdap.api.moudle.formconfig.pojo.FormComponent;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class FormDataUtil {


    public static List<FormComponent> getChildFc(List<FormComponent> formComponents) {
        List<FormComponent> childsAndFcs = new ArrayList<>();
        // childsAndFcs.addAll(formComponents);
        for (FormComponent fc : formComponents) {
            List<FormComponent> children = fc.getChildren();
            if (CollectionUtils.isNotEmpty(children)) {
                List<FormComponent> childFc = getChildFc(children);
                // 把子数据都加上
                childsAndFcs.addAll(children);
                childsAndFcs.addAll(childFc);
                // 加入当前数据
                childsAndFcs.add(fc);
            }
            List<FormComponent> tableColumn = fc.getTableColumn();
            if (CollectionUtils.isNotEmpty(tableColumn)) {
                List<FormComponent> childFc = getChildFc(tableColumn);
                // 把子数据都加上
                childsAndFcs.addAll(tableColumn);
                childsAndFcs.addAll(tableColumn);
                // 加入当前数据
                childsAndFcs.add(fc);
            }

        }
        List<FormComponent> collect = childsAndFcs.stream().distinct().collect(Collectors.toList());
        return collect;
    }
}
